﻿using System;
using System.ComponentModel;
using System.Data.Linq;
using System.Data.Linq.Mapping;
using Model.Interfaces;
using Model.ModelClasses;
using Model.ModelEventArgs;

namespace Data.MappingClasses
{
    [TableAttribute(Name = "dbo.Country")]
    public partial class Country : AbstractMappingClass, INotifyPropertyChanging, INotifyPropertyChanged
    {
        public event EventHandler<ModelItemEventArgs> AddedTownEvent;
        public event EventHandler<ModelItemEventArgs> RemovedTownEvent;

        private static PropertyChangingEventArgs emptyChangingEventArgs = new PropertyChangingEventArgs(String.Empty);

        private short _idCountry;

        private string _nameCountry;

        private EntitySet<Town> _Towns;

        #region Extensibility Method Definitions
        partial void OnLoaded();
        partial void OnValidate(ChangeAction action);
        partial void OnCreated();
        partial void OnidCountryChanging(short value);
        partial void OnidCountryChanged();
        partial void OnnameCountryChanging(string value);
        partial void OnnameCountryChanged();
        #endregion

        public Country()
        {
            this._Towns = new EntitySet<Town>(AttachTowns, DetachTowns);
            OnCreated();
        }

        [ColumnAttribute(Storage = "_idCountry", AutoSync = AutoSync.OnInsert, DbType = "SmallInt NOT NULL IDENTITY", IsPrimaryKey = true, IsDbGenerated = true)]
        public short idCountry
        {
            get
            {
                return this._idCountry;
            }
            set
            {
                if ((this._idCountry != value))
                {
                    this.OnidCountryChanging(value);
                    this.SendPropertyChanging();
                    this._idCountry = value;
                    this.SendPropertyChanged("idCountry");
                    this.OnidCountryChanged();
                }
            }
        }

        [ColumnAttribute(Storage = "_nameCountry", DbType = "VarChar(100) NOT NULL", CanBeNull = false)]
        public string NameCountry
        {
            get
            {
                return this._nameCountry;
            }
            set
            {
                if ((this._nameCountry != value))
                {
                    this.OnnameCountryChanging(value);
                    this.SendPropertyChanging();
                    this._nameCountry = value;
                    this.SendPropertyChanged("nameCountry");
                    this.OnnameCountryChanged();
                }
            }
        }

        [AssociationAttribute(Name = "Country_Town", Storage = "_Towns", ThisKey = "idCountry", OtherKey = "refCountry")]
        public EntitySet<Town> Towns
        {
            get
            {
                return this._Towns;
            }
            set
            {
                this._Towns.Assign(value);
            }
        }

        public event PropertyChangingEventHandler PropertyChanging;

        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void SendPropertyChanging()
        {
            if ((this.PropertyChanging != null))
            {
                this.PropertyChanging(this, emptyChangingEventArgs);
            }
        }

        protected virtual void SendPropertyChanged(String propertyName)
        {
            if ((this.PropertyChanged != null))
            {
                this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        private void AttachTowns(Town entity)
        {
            this.SendPropertyChanging();
            entity.Country = this;
        }

        private void DetachTowns(Town entity)
        {
            this.SendPropertyChanging();
            entity.Country = null;
        }

        /*
        public override AbstractModel GetPureModel()
        {
            var pureModel = new Model.ModelClasses.Country {Name = _nameCountry};
            foreach (var town in _Towns)
                pureModel.Towns.Add(town.nameTown);
            pureModel.UpdateModelEvent += UpdateModelEventHandler;
            pureModel.Towns.AddedItemEvent += AddedItemEventHandler;
            pureModel.Towns.RemovedItemEvent += RemovedItemEventHandler;
            return pureModel;
        }
        */
        /*
        private void RemovedItemEventHandler(object sender, StringEventArgs e)
        {
            if (RemoveTownEvent != null)
                RemoveTownEvent(this, e);
        }

        private void AddedItemEventHandler(object sender, StringEventArgs e)
        {
            Towns.Add(new Town{Country = this, nameTown = e.Body});
            UpdateData();
        }

        private void UpdateModelEventHandler(object sender, EventArgs e)
        {
            NameCountry = ((Model.ModelClasses.Country)sender).Name;
            UpdateData();
        }
        */

        public override AbstractModel PureModel
        {
            set
            {
                if (_pureModel != null)
                {
                    _pureModel.UpdateModelEvent -= UpdateCountryEventHandler;
                    ((ICountry)_pureModel).Towns.AddedItemEvent -= AddedItemEventHandler;
                    ((ICountry)_pureModel).Towns.RemovedItemEvent -= RemovedItemEventHandler;
                }

                if (value != null)
                {
                    value.UpdateModelEvent += UpdateCountryEventHandler;
                    ((ICountry)value).Towns.AddedItemEvent += AddedItemEventHandler;
                    ((ICountry)value).Towns.RemovedItemEvent += RemovedItemEventHandler;
                }

                _pureModel = value;
            }
        }

        private void RemovedItemEventHandler(object sender, ModelItemEventArgs e)
        {
            if (RemovedTownEvent != null)
                RemovedTownEvent(this, e);
        }

        private void AddedItemEventHandler(object sender, ModelItemEventArgs e)
        {
            if(AddedTownEvent != null)
                AddedTownEvent(this, e);
        }

        private void UpdateCountryEventHandler(object sender, EventArgs e)
        {
            NameCountry = ((ICountry) _pureModel).Name;
            UpdateData();
        }
    }
}